package com.taotaojs.mybootsuper.service2impl.impl;

import com.taotaojs.mybootsuper.bean.BaseBean;
import com.taotaojs.mybootsuper.bean.KeyValueBean;
import com.taotaojs.mybootsuper.bean.KeyValueList;
import com.taotaojs.mybootsuper.mapper.MySqlBuild;
import com.taotaojs.mybootsuper.service2impl.IMyComService;
import com.taotaojs.util.CollectionUtil;
import com.taotaojs.util.date.CalendarUtil;
import com.taotaojs.util.reflect.ReflectUtil;

import java.lang.reflect.Method;
import java.util.*;

/**
 * 通用Service继承父类
 * @author: TaoTaojs
 * @Date: 2020年03月20日15:49:46
 * @Description:
 * 主要是统一继承类，减少配置
 * @关联类:
 * @参考链接：
 */
public class MyComServiceImpl<T extends BaseBean> extends MyBasicServiceImpl<T> implements IMyComService<T> {

    @Override
    public List<T> getComList(MySqlBuild<T> mysql){
        return myMapper.selectObjList(mysql);
    }

    @Override
    public<K> Map<K, T> getComMap(MySqlBuild<T> mysql){
        return (Map<K, T>) myMapper.selectObjMap(mysql);
    }

    @Override
    public List<T> findByIds(Collection<Integer> ids){
        KeyValueList<String, Collection<?>> idsMap = new KeyValueList<>();
        idsMap.add(KeyValueBean.<String, Collection<?>>builder()
                .key("id")
                .value(ids)
                .build());
        return myMapper.selectObjList(MySqlBuild.<T>builder()
                .idsMapList(idsMap)
                .build());
    }

    @Override
    public List<T> findByObjList(MySqlBuild<T> mysql){
        return myMapper.selectObjList(mysql);
    }

    @Override
    public Map<Integer, T> findByObjMap(Collection<Integer> ids){
        if(CollectionUtil.isEmpty(ids)) {
            return new HashMap<>(2);
        }
        KeyValueList<String, Collection<?>> idsMap = new KeyValueList<>();
        idsMap.add(KeyValueBean.<String, Collection<?>>builder()
                .key("id")
                .value(ids)
                .build());
        return myMapper.selectObjMap(MySqlBuild.<T>builder()
                .idsMapList(idsMap)
                .build());
    }
    @Override
    public Map<Integer, T> findByObjMap(MySqlBuild<T> mysql){
        return myMapper.selectObjMap(mysql);
    }

    @Override
    public<K> Map<K, T> findObjMap(MySqlBuild<T> mysql, String key) {
        Map<K,T> map = new HashMap<>(2);
        List<T> list = findByObjList(mysql);
        Method getMethod = ReflectUtil.getGetMethod(clazz, key);
        for (T t : list) {
            K k = (K) ReflectUtil.invokeMethod(t, getMethod);
            map.put(k, t);
        }
        return map;
    }

    @Override
    public List<T> findByStartAndStopDate(Date startDate, Date stopDate){
        return findByStartAndStopDate(startDate, stopDate, "create_date");
    }
    @Override
    public List<T> findByStartAndStopDate(Date startDate, Date stopDate, String dateColumn){
        List<MySqlBuild.Where> where = new ArrayList<>();
        where.add(MySqlBuild.Where.builder()
                .whereStr(dateColumn + " >= '" + CalendarUtil.formatDate(startDate, CalendarUtil.FORMAT_ALL)+ "'")
                .build());
        where.add(MySqlBuild.Where.builder()
                .whereStr(dateColumn + " < '" + CalendarUtil.formatDate(stopDate, CalendarUtil.FORMAT_ALL) + "'")
                .build());

        return findByObjList(MySqlBuild.<T>builder()
                .dataWhere(where)
                .build());

    }

}
